package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.pojo.EduChapter;
import com.atguigu.eduservice.pojo.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-20
 */
@RestController
@RequestMapping(value = "/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;


    //课程大纲的列表，根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
    public R getCHapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);

    }
    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
         chapterService.save(eduChapter);
         return R.ok();
    }

    //修改方法，根据章节的id查询
    @GetMapping(value = "getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable("chapterId") String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }
    //修改

    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除方法
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
       boolean flag =  chapterService.deleteChapter(chapterId);
       if (flag) {
           return R.ok();
       }else{
           return R.error();
       }
    }
}


